package controllers

import (
	"github.com/astaxie/beego"
	"myapp/models"
	"fmt"
)


type CategoryController struct{
	beego.Controller
}


func (c *CategoryController) Get(){

	op := c.Input().Get("op")

	fmt.Print("888888888888888888888888888888888888888888888888")
	fmt.Printf("%v",op)
	switch op {
	case "add":
		fmt.Print("7777777777777777777777777777777777777777777777")
		catename := c.Input().Get("catename")

		if len(catename) ==0{
			break
		}

		fmt.Print(catename)
		err := models.AddCategory(catename)

		if err !=nil{
			beego.Error(err)
		}

		c.Redirect("/category",301)
		return


	case "del":
		id :=c.Input().Get("id")
		if len(id)==0{
			break
		}
		err := models.DelCategory(id)

		if err != nil{
			beego.Error(err)
		}
		c.Redirect("/category",301)
		return

	}


	c.Data["IsCategory"] = true
	c.TplName = "category.html"

	var err error

	c.Data["Categories"],err = models.GetAllCategories()

	if err !=nil{
		beego.Error(err)
	}

	c.Data["IsLogin"] = checkAccount(c.Ctx)
}